package com.cjjd.c.feature.subscribe.info;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.subscribe.BookOrderModel;
import com.cjjd.c.bean.subscribe.HotelRoomShowModel;
import com.cjjd.c.bean.subscribe.HotelRoomShowModel.HotelRoomTypeShowModel;
import com.cjjd.c.feature.subscribe.write_order.WriteOrderActivity;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.http.RetrofitSingleton;
import com.cjjd.c.utils.ImageUtil;
import com.cjjd.c.utils.SharePrefUtil;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;


/**
 * ----------------------------------
 * Created by xyd on 2017/8/30.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class HotelDetailExpandableListAdapter<G, C> extends BaseExpandableListAdapter {

    private ArrayList<G> dataI;
    private ArrayList<ArrayList<C>> dataG;
    private Context mContext;

    public HotelDetailExpandableListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public int getGroupCount() {
        return dataI.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return dataG.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return dataI.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return dataG.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        ViewHolderGroup viewHolderGroup = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.group_select_hotel_type, parent, false);
            viewHolderGroup = new ViewHolderGroup(convertView);
            convertView.setTag(viewHolderGroup);
        } else {
            viewHolderGroup = (ViewHolderGroup) convertView.getTag();
        }

        final HotelRoomShowModel groupItemData = (HotelRoomShowModel) getGroup(groupPosition);


        ImageUtil.displayImage(groupItemData.picPth, viewHolderGroup.iv_hotel_type);
        viewHolderGroup.tv_hotel_type_name.setText(groupItemData.roomTypeName);
        viewHolderGroup.tv_hotel_type_description.setText(groupItemData.appsCount + "条评论");
        viewHolderGroup.tv_nomal_price.setText("￥ " + groupItemData.price + " ");

        viewHolderGroup.tv_hotel_type_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, HotelDeviceActivity.class);
                // TODO: 2017/12/18 这里最好在接口里加一个hotelUuid
                intent.putExtra("hotelUuid", "0424252f017e49289b68c9987d6b6e0d");
                intent.putExtra("roomTypeUuid", groupItemData.roomTypeUuid);
                mContext.startActivity(intent);
            }
        });

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ViewHolderChildChange viewHolderChildChange = null;
        ViewHolderChild viewHolderChild = null;
        if (convertView == null) {
            if (from == From_Change_Order) {
                // 来自修改订单的选房
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_select_hotel_type_change, parent, false);
                viewHolderChildChange = new ViewHolderChildChange(convertView);
                convertView.setTag(viewHolderChildChange);
            } else {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_select_hotel_type, parent, false);
                viewHolderChild = new ViewHolderChild(convertView);
                convertView.setTag(viewHolderChild);
            }

        } else {
            if (from == From_Change_Order) {
                viewHolderChildChange = (ViewHolderChildChange) convertView.getTag();
            } else {
                viewHolderChild = (ViewHolderChild) convertView.getTag();
            }
        }

        final HotelRoomShowModel groupItemData = (HotelRoomShowModel) getGroup(groupPosition);
        final HotelRoomTypeShowModel childItemData = (HotelRoomTypeShowModel) getChild(groupPosition, childPosition);

        if (from == From_Change_Order) {
            // 来自修改订单的选房
            viewHolderChildChange.tv_dif_price.setText(childItemData.tag);
            viewHolderChildChange.tv_nomal_price.setText("￥" + childItemData.totalPrice);
            viewHolderChildChange.cb_child_item.setChecked(childItemData.isSelected);
        } else {
            viewHolderChild.tv_dif_price.setText(childItemData.tag);
            viewHolderChild.tv_nomal_price.setText("￥" + childItemData.totalPrice);
            // 来酒店详情的选房
            viewHolderChild.btnSubscribe.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    toBookOrder(groupItemData.roomTypeUuid, childItemData.priceTypeUuid);
                }
            });
        }



        return convertView;
    }


    private String hotelUuid = "";
    private String startDay = "";
    private String endDay = "";


    public void setPreString(String hotelUuid,String startDay,String endDay){
        this.hotelUuid = hotelUuid;
        this.startDay = startDay;
        this.endDay = endDay;
    }

    /**
     * 生成预订单
     *
     * @param roomTypeUuid
     * @param priceTypeUuid
     */
    private void toBookOrder(final String roomTypeUuid, final String priceTypeUuid) {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        // TODO: 2017/12/19 这里需要重写
        h1.put("hotelUuid", hotelUuid);    //先写死
        h1.put("roomTypeUuid", roomTypeUuid);    //房型
        h1.put("priceTypeUuid", priceTypeUuid);    //房间类型
        h1.put("startDate", startDay);    //开始时间
        h1.put("endDate", endDay);    //结束时间

        Call<HttpResult<BookOrderModel>> call = RetrofitSingleton.getService(mContext).toBookOrder(JsonUtils.convertParam("toBookOrder", h1));
        call.enqueue(new CallbackImpl<BookOrderModel>() {
            @Override
            public void responseSuccess(Call<HttpResult<BookOrderModel>> call, Response<HttpResult<BookOrderModel>> response) {

                BookOrderModel bookOrderModel = response.body().getData();
                Intent intent = new Intent(mContext, WriteOrderActivity.class);
                intent.putExtra("bookOrderModel", bookOrderModel);
                intent.putExtra("hotelUuid", hotelUuid);
                intent.putExtra("roomTypeUuid", roomTypeUuid);
                intent.putExtra("priceTypeUuid", priceTypeUuid);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    public void setGroupData(ArrayList<ArrayList<C>> listG) {
        this.dataG = listG;
    }

    public void setGroupTittleData(ArrayList<G> listI) {
        this.dataI = listI;
    }

    public static final int From_Change_Order = 0x00000001;
    private int from = -1;

    public void setFrom(int from) {
        this.from = from;
    }


    static class ViewHolderGroup {
        @Bind(R.id.iv_hotel_type)
        ImageView iv_hotel_type;
        @Bind(R.id.tv_hotel_type_name)
        TextView tv_hotel_type_name;
        @Bind(R.id.tv_hotel_type_description)
        TextView tv_hotel_type_description;
        @Bind(R.id.tv_nomal_price)
        TextView tv_nomal_price;

        ViewHolderGroup(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolderChild {

        @Bind(R.id.tv_dif_price)
        TextView tv_dif_price;
        @Bind(R.id.tv_nomal_price)
        TextView tv_nomal_price;
        @Bind(R.id.btn_subscribe)
        Button btnSubscribe;

        ViewHolderChild(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolderChildChange {

        @Bind(R.id.tv_dif_price)
        TextView tv_dif_price;
        @Bind(R.id.tv_nomal_price)
        TextView tv_nomal_price;
        @Bind(R.id.cb_child_item)
        CheckBox cb_child_item;


        ViewHolderChildChange(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
